package com.geese.module.ceo.controller;

import java.util.List;
import java.util.Map;

import com.geese.common.entity.Business;
import com.geese.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Campus;
import com.geese.common.entity.Courier;
import com.geese.common.util.BeanUtil;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.module.ceo.service.CampusService;
import com.geese.module.ceo.service.CourierService;
import com.geese.module.ceo.vo.CampusQuery;
import com.geese.module.ceo.vo.CampusVO;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author YeFangYi 
 */
@Controller
@RequestMapping(value = "/ceo")
@Scope("prototype")
public class CampusController extends BaseController {

    @Autowired 
    private UserService userService;
    @Autowired
    private CampusService campusService;
    @Autowired
    private CourierService courierService;


    /**
     * 新增校区
     */
    @PostMapping("/campus/save")
    @ResponseBody
    public String campusSave(@RequestBody CampusVO campusVO, @RequestParam String token){
    	if(StringUtils.isEmpty(campusVO.getName())) {
    		throw new BusinessException(120018);
    	}
    	Campus campus = BeanUtil.map(campusVO, Campus.class);
    	
    	SecurityUser user = userService.getLoginUser(token);
    	Courier courier = courierService.findByUserId(user.getUserId());
    	campus.setSchoolId(courier.getSchoolId());
    	campusService.save(campus);
        return response();
    }

    /**
     * 批量删除校区
     */
    @PostMapping("/campus/batchRemove")
    @ResponseBody
    public String campusBatchRemove(@RequestParam String token, @RequestParam String ids){
    	List<String> tempIds = Splitter.on(",").trimResults().splitToList(ids);
    	if(CollectionUtils.isEmpty(tempIds)) {
    		throw new BusinessException(120016);
    	}
    	List<Integer> campusIds = Lists.transform(checkIds(tempIds), new Function<String, Integer>() {
			@Override
			public Integer apply(String tempId) {
				return Integer.valueOf(tempId);
			}
		});
    	userService.checkUser(token);
    	campusService.batchRemove(campusIds);
        return response();
    }

    /**
     * 修改校区
     */
    @PostMapping("/campus/update")
    @ResponseBody
    public String campusUpdate(@RequestBody CampusVO campusVO, @RequestParam String token){
    	if(!StringUtils.isNumeric(campusVO.getId())) {
    		throw new BusinessException(120039);
    	}
    	if(StringUtils.isEmpty(campusVO.getName())) {
    		throw new BusinessException(120018);
    	}
    	
    	userService.checkUser(token);
    	Campus campus = BeanUtil.map(campusVO, Campus.class);
    	campusService.update(campus);
        return response();
    }
    
    /**
     * 校区列表
     */
    @GetMapping("/campus/listInPage")
    @ResponseBody
    public String campusList(CampusQuery query) {
    	userService.checkUser(query.getToken());
    	SecurityUser user = userService.getLoginUser(query.getToken());
    	Courier courier = courierService.findByUserId(user.getUserId());
    	
    	Page page = getPageInfo(query);
    	Paginator<Campus> pageList = campusService.listAllByQuery(courier.getSchoolId(), query.getName(), page);
    	Map<String, Object> result = Maps.newHashMap();
    	result.put(Constants.PAGE_INFO, pageList.getPageResult());
    	result.put("campusList", pageList.getResults());
        return response(result);
    }
}
